<template>
  <div class="app-container">
    <div class="attendance-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item label="输入搜索：" prop="name" class="el-form-item">
          <el-input v-model="queryParams.name" placeholder="姓名" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item class="el-form-item">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table ref="table" v-loading="loading" :data="list" :selection.sync="selectedRows"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="车辆图片" align="center">
          <template slot-scope="{ row }">
            <el-image style="width: 50px; height: 50px" :src="formattedHeadIcon(row.imageUrl)" fit="fill"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="车辆号码" align="center" prop="carNumber" :show-overflow-tooltip="true" />
        <el-table-column label="车辆名称" align="center" prop="name" :show-overflow-tooltip="true" />
        <el-table-column label="车辆分类" align="center">
          <template slot-scope="{ row }">
            <div v-if="row.classify == 0">三轮车</div>
            <div v-if="row.classify == 1">清运车</div>
          </template>
        </el-table-column>
        <el-table-column label="驾驶人" align="center" prop="driver" />
        <el-table-column label="手机号" align="center" prop="driverPhone" />
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
      <div class="checked-container">
        <el-checkbox v-model="checked" @change=changeChecked()>全选</el-checkbox>
        <el-popover placement="top" width="160" v-model="visible">
          <p>是否确定绑定？</p>
          <div style="text-align: right; margin: 0">
            <el-button size="mini" type="text" @click="visible = false">取消</el-button>
            <el-button type="primary" size="mini" @click="handleSure">确定</el-button>
          </div>
          <el-button slot="reference" size="mini" class="btn-checked">绑定</el-button>
        </el-popover>
      </div>
    </div>
  </div>
</template>

<script>
import { listCars, boundCars, listCarsIds } from "@/api/system/area";
const prefix = process.env.VUE_APP_CDN_HOST;
export default {
  name: "Logininfor",
  data() {
    return {
      visible: false,
      checked: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 存储选中的行
      selectedRows: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaId: undefined
      },
      carIds: []
    };
  },
  created() {
    if (this.$route.query && this.$route.query.areaId) {
      this.queryParams.areaId = this.$route.query.areaId
    };
    listCarsIds(this.queryParams).then(response => {
      this.carIds = response.data
      this.getList();
    }
    );
  },
  methods: {
    formattedHeadIcon(icon) {
      return prefix + icon;
    },
    getList() {
      this.loading = true;
      listCars(this.queryParams).then(response => {
        this.list = response.data.records;
        this.total = response.data.total;
        this.loading = false;
        this.selectedRows = this.list.filter(item => this.carIds.includes(item.id));
        this.$nextTick(() => {
          this.$refs.table.clearSelection();
          this.selectedRows.forEach(row => {
            this.$refs.table.toggleRowSelection(row, true);
          });
        });
      }
      );
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1
    },
    //全选
    changeChecked() {
      if (this.checked) {
        this.$refs.multipleTable.toggleAllSelection(); // 全选
      } else {
        this.$refs.multipleTable.clearSelection(); // 取消选择
      }
    },
    handleSelectionChange(selection) {
      this.selectedRows = selection;
      if (selection.length === this.list.length) {
        this.checked = true;
      } else {
        this.checked = false;
      }
    },
    //批量绑定
    handleSure() {
      boundCars({ carIds: this.selectedRows.map(item => item.id), areaId: this.queryParams.areaId }).then(response => {
        this.$modal.msgSuccess("绑定成功");
        this.$router.push({ path: '/work/region' })
      });
      this.visible = false
    },
  }
};
</script>
<style lang="scss" scoped>
.attendance-container {
  .el-form-item {
    margin-left: 20px;
  }

  .checked-container {
    position: relative;
    top: -30px;
    left: 20px;

    .btn-checked {
      margin-left: 10px;
    }
  }
}
</style>
